# Alparslan Sari                 December 2012
# asari@udel.edu
# 2bit direct encoding
# Compression ration is 4:1
# Left over bits will represented in file name.

import os
import sys
import time
import array

# This function reads a file line by line
# and removes \n
# returns the file data
def read_file(filename):
    data= ""
    file = open(filename)
    while 1:
        line = file.readline()
        line = line.rstrip() # remove new line
        data = data + line
        if not line:
            break
    return data

def write_data(key, filename):
    #print key
    key = key.replace('A','00')
    key = key.replace('T','01')
    key = key.replace('G','11')
    key = key.replace('C','10')
    data = array.array('B')  # create array of bytes.
    chunk = ""
    for letter in key:
        chunk = chunk + letter
        if len(chunk) == 8:
            data.append(int(chunk, 2))
            chunk = ""
    if len(chunk) > 0:
        filename = filename + '.'+chunk+'.bin'
    else:
        filename = filename + '.bin'
    f = file(filename, 'wb')
    #print data
    data.tofile(f)
    f.close()

start_time = time.time()
rfile = sys.argv[1]
data = read_file(rfile)
# 1. compression
write_data(data,rfile)
print time.time() - start_time, "seconds"
